package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductVo;
import cn.itcast.domain.vo.ExportVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.utils.UUIDUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("cargo/export")
public class ExportController extends BaseController {

    @Reference
    ContractService contractService;

    @Reference
    ExportService exportService;

    @Reference
    ExportProductService exportProductService;

    @Reference
    PackingService packingService;

    @RequestMapping(value = "contractList",name = "查询已上报的合同")
    public String contractList(
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "10")int size){


        ContractExample contractExample = new ContractExample();
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        criteria.andStateEqualTo(1);

        contractExample.setOrderByClause("ship_time desc");
        PageInfo pageInfo = contractService.findAll(page, size, contractExample); //若有合同专员的话,就里面就不需要根据登陆者查询个人的还是部门的
        request.setAttribute("page",pageInfo);

        return "cargo/export/export-contractList";
    }

    @RequestMapping(value = "list",name = "报运单列表")
    public String findAll(
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "10")int size){

        //设置条件
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        exportExample.setOrderByClause("create_time desc");

        PageInfo pageInfo = exportService.findAll(page, size, exportExample);
        request.setAttribute("page",pageInfo);


        return "cargo/export/export-list";
    }


    @RequestMapping(value = "toExport",name = "跳转到报运页面")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping(value = "edit",name = "新增或者修改报运单")
    public String edit(Export export){

        if (StringUtils.isBlank(export.getId())) {
            //新增报运单
            export.setCompanyId(getCompanyId());
            export.setCompanyName(getCompanyName());

            exportService.save(export);
            //出于权限的考虑,新增成功之后,跳转到奥合同列表页面
            return "redirect:/cargo/export/contractList.do";
        }else{
            //更新报运单
            exportService.update(export);
            return "redirect:/cargo/export/list.do";
        }
    }

    @RequestMapping(value = "toUpdate",name = "跳转到报运单更新页面")
    public String toUpdate(String id){
        //1.查询报运单信息,回显
        Export export = exportService.findById(id);
        request.setAttribute("export",export);

        //2.查询改报运单涉及的所有商品,回显
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);

        List<ExportProduct> epList = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps",epList);

        return "cargo/export/export-update";
    }

    @RequestMapping(value = "submit",name = "提交报运单")
    public String submit(String id){
        Export export = new Export();
        export.setId(id);
        export.setState(1);

        exportService.update(export);

        return "redirect:/cargo/export/list.do";
    }


    @RequestMapping(value = "cancel",name = "取消报运单")
    public String cancel(String id){
        Export export = new Export();
        export.setId(id);
        export.setState(0);

        exportService.update(export);

        return "redirect:/cargo/export/list.do";
    }


    @RequestMapping(value = "exportE",name = "海关电子报运")
    public String exportE(String id){
        //1. 查询到报运单,转换成ExportVo对象
        Export export = exportService.findById(id);

        //1.1 创建exportVo对象
        ExportVo exportVo = new ExportVo();
        //1.2 拷贝export中的数据到exportVo对象
        BeanUtils.copyProperties(export,exportVo);
        //1.3 给vo对象设置报运单的id,方便后期查询报运的结果
        exportVo.setExportId(id);

        //2. 查询到报运单涉及商品list,遍历list,获取到每个报运商品
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);

        //常见报运商品vo的list集合
        List<ExportProductVo> exportProductVoList = new ArrayList<>();

        if (list!=null&&list.size()>0) {
            for (ExportProduct ep : list) {
                //3. 将报运商品转换成ExportProductVo对象
                ExportProductVo epvo = new ExportProductVo();
                BeanUtils.copyProperties(ep,epvo);

                //4.将报运商品的id设置给vo中报运商品的id
                epvo.setExportProductId(ep.getId());

                //5.将每个ExportProductVo对象放入list中
                exportProductVoList.add(epvo);
            }
        }

        //5. 将商品vo的list设置到ExportVo对象
        exportVo.setProducts(exportProductVoList);

        //6. 使用webClient进行报关
        WebClient webClient = WebClient.create("http://localhost:8989/ws/export/user");
        webClient.post(exportVo);

        //////////////////////报运结果查询/////////////////////////////
        /*//1. 使用webClient进行查询,参数就是报运单的id
        webClient = WebClient.create("http://localhost:9999/ws/export/user/"+id);
        //2. 接受返回值ExportResult对象
        ExportResult exportResult = webClient.get(ExportResult.class);
        //3. 调用exportService.updateByExportResult(result),修改报运单的状态和商品的信息
        exportService.updateByExportResult(exportResult);*/

        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "generate",name = "生成装箱单")
    public String generate(String id){
        //1. 查询到报运单,转换成Packing对象
        Export export = exportService.findById(id);
        ExportProduct exportProduct = exportProductService.findById(id);

        //1.1 创建packing对象
        Packing packing = new Packing();
        //1.2 拷贝export中的数据到packing对象
        BeanUtils.copyProperties(export,packing);
        //BeanUtils.copyProperties(exportProduct,packing);

        //1.3 给packing对象设置装箱单的id,方便后期查询装箱单
        packing.setExportId(id);

        //1.4后台自动计算传入
        packing.setExportNos(UUIDUtils.getId());
        packing.setMeasurements(String.valueOf(export.getMeasurements()));//总体积

        packing.setNetWeights(String.valueOf(export.getGrossWeights()));//总毛重

        //packing.setGrossWeights(String.valueOf(exportProduct.getNetWeight()));//总净重
	    //System.out.println(packing);

        packing.setPackingMoney(String.valueOf(export.getGrossWeights()*520));//装箱费用=总毛重*520
	    //System.out.println(packing);


        request.setAttribute("packing",packing);

       /* //2.查询装箱单涉及的所有商品,回显
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andExportIdEqualTo(id);

        List<Packing> list = packingService.findAll(packingExample);
        request.setAttribute("list",list);
*/
        return "cargo/export/export-generate";


    }
}
